package com.example.platfrom.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.platfrom.service.ItripHotelService;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.SolrDocumentList;
import org.example.mapper.*;
import org.example.pojo.ItripComment;
import org.example.pojo.ItripHotel;
import org.example.pojo.ItripHotelRoom;
import org.example.pojo.ItripImage;
import org.example.utils.SolrUtil;
import org.example.vo.recommend.ItripHotel_Recommend;
import org.example.vo.recommend.hotel.Recommend_Hotel;
import org.example.vo.recommend.hotelrecommend.Recommend_Comment;
import org.example.vo.solr.SearchHotel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class ItripHotelServiceImpl implements ItripHotelService {
    @Autowired
    private ItripHotelMapper hotelMapper;
    @Autowired
    private ItripImageMapper imageMapper;
    @Autowired
    private ItripHotelRoomMapper hotelRoomMapper;
    @Autowired
    private ItripCommentMapper commentMapper;
    private QueryWrapper wrapper;

    @Override
    public IPage queryAll(Integer pageNum, String city) {
        Page<ItripHotel> page = new Page<>(pageNum,3);
        wrapper = new QueryWrapper();
        wrapper.eq("redundantCityName",city+"市");
        IPage iPage = hotelMapper.selectPage(page, wrapper);
//        System.out.println("总条数："+iPage.getTotal());
//        System.out.println("总页数："+iPage.getPages());
        return iPage;
    }

    private final String CITYSUFFIX = "市";
    @Override
    public List<ItripHotel_Recommend> queryHotelRecommend(String cityPrefix) {
        String city = cityPrefix + CITYSUFFIX;
        List<Recommend_Hotel> recommend_hotels = hotelMapper.queryByCityName(city);
        List<ItripHotel_Recommend> recommendList = new ArrayList<>();
        double score = 0;
        Long countComment = 0L;
        for (int i = 0; i < recommend_hotels.size(); i++) {
            Recommend_Comment recommend_comment = commentMapper.queryByHotelComment(recommend_hotels.get(i).getId());
            if (recommend_comment.getId() != null){//代表该酒店有评论
                score = recommend_comment.getScore();
                countComment = recommend_comment.getCountComment();
            }else{
                score = 0; countComment = 0L;
            }
            recommendList.add(new ItripHotel_Recommend(
                    recommend_hotels.get(i).getId(),//酒店编号
                    recommend_hotels.get(i).getHotelName(),//酒店名称
                    recommend_hotels.get(i).getHotelLevel(),//酒店星级
                    score,//酒店综评
                    countComment,//酒店评论总条数
                    recommend_hotels.get(i).getPrice(),//酒店最低价格
                    recommend_hotels.get(i).getImgUrl(),//酒店图片
                    recommend_hotels.get(i).getRecommendOrder()//竞价排名顺序
            ));
            System.out.println(recommendList.get(i));
        }
        return recommendList;
    }

    @Override
    public List<SearchHotel> queryBySolrCondition(String hotelName, String cityName) {
        String params = "";
        if ((hotelName == null || hotelName.equals("")) && (cityName == null || cityName.equals(""))){
            params = "*:*";
        }else {
            if (hotelName == null || hotelName.equals("")) {
                if (cityName != null && !cityName.equals("")) {
                    params = "redundantCityName:"+cityName+CITYSUFFIX;
                }
            } else {
                if (cityName == null || cityName.equals("")) {
                    params = "hotelName:"+hotelName;
                } else {
                    params = "hotelName:"+hotelName+" AND redundantCityName:"+cityName+CITYSUFFIX;
                }
            }
        }
        SolrQuery solrQuery = SolrUtil.getSolrQuery(ItripHotel.class,params);
        solrQuery.setRows(Integer.MAX_VALUE);
        SolrDocumentList documentList = SolrUtil.getSolrDocumentList(solrQuery);
        Field[] fields = ItripHotel.class.getDeclaredFields();
        List<SearchHotel> searchHotels = new ArrayList<>();
        for (SolrDocument result : documentList) {
            Long searchHotelId = Long.valueOf(result.getFieldValue(fields[0].getName()).toString());
            SearchHotel searchHotel = hotelMapper.queryByHotelId(searchHotelId);
            Recommend_Comment recommend_comment = commentMapper.queryByHotelComment(searchHotelId);
            if (recommend_comment.getId() == null){//代表该酒店无评论
                recommend_comment = new Recommend_Comment(searchHotelId,0L,0);
            }
            searchHotel.setRecommend_comment(recommend_comment);
            searchHotels.add(searchHotel);
        }
        SolrUtil.closeClient();
        return searchHotels;
    }

    @Override
    public List<SearchHotel> queryFallBack(String hotelName,String cityName) {
        if (cityName == null || cityName.equals("")){
            cityName = "";
        }else{
            cityName = cityName + CITYSUFFIX;
        }
        List<SearchHotel> searchHotels = hotelMapper.queryByHotelNameAndCity(hotelName,cityName);
        for (SearchHotel searchHotel : searchHotels) {
            Recommend_Comment recommend_comment = commentMapper.queryByHotelComment(searchHotel.getId());
            if (recommend_comment.getId() == null){//代表该酒店无评论
                recommend_comment = new Recommend_Comment(searchHotel.getId(),0L,0);
            }
            searchHotel.setRecommend_comment(recommend_comment);
        }
        return searchHotels;
    }

    @Override
    public Map<String,Object> queryHotelDetails(Long id) {
        Map<String,Object> map = null;
        wrapper = new QueryWrapper();
        ItripHotel itripHotel = hotelMapper.selectById(id);
        if (itripHotel!=null){
            map = new HashMap<>();
            map.put("hotel",itripHotel);
            wrapper.eq("targetId",itripHotel.getId());
            List<ItripImage> images = imageMapper.selectList(wrapper);
            if (images.size()>0 && images!=null){
                map.put("images",images);
            }
            wrapper.clear();wrapper.eq("hotelId",itripHotel.getId());
            List<ItripHotelRoom> hotelRooms = hotelRoomMapper.selectList(wrapper);
            if (hotelRooms.size()>0 && hotelRooms!=null){
                map.put("hotelRooms",hotelRooms);
            }
            List<ItripComment> hotelComment = commentMapper.selectList(wrapper);
            if (hotelComment.size()>0 && hotelComment!=null){
                map.put("hotelComment",hotelComment);
            }
        }
        return map;
    }
}
